<?php
namespace App\library\repositories;


use App\library\services\BaseServices;
use App\Models\Agent;
use App\Models\WxUser;

class Login extends BaseServices
{
    /**
     * Author:Yingbo Sun
     * 授权登录获取用户信息
     * @param $params
     * @return array
     */
    public static function login($params,$uid,$type)
    {
        if (empty($uid)){
            return self::fail('参数错误');
        }
        $params['last_time'] = time();
        unset($params['s']);
        if ($type == 'user'){
            $field = ['id','nick_name','phone','avatar','sex'];
            $model = new WxUser();
        }else{
            $field = ['id','nick_name','phone','avatar','company','new_house_num','second_hand_house_num','rent_house_num','is_first'];
            $params['is_first'] = 1;
            $model = new Agent();
        }
        return self::firstLogin($model,$params,$uid,$field);
    }

    public static function firstLogin($model,$params,$uid,$field)
    {
        $res = $model->where('id',$uid)->update($params);
        if ($res != false){
            $data = $model->where('id',$uid)->first($field)->toArray();
            $data['id'] = encrypt($data['id']);
            return self::success('登录成功',$data);
        }else{
            return self::fail('授权登录失败');
        }
    }

    /**
     * Author:Yingbo Sun
     * 经纪人登录 获取用户信息
     * @param $phone
     * @param $aid
     * @return array
     */
    public static function agentLogin($phone,$aid)
    {
        if (empty($phone) || empty($aid)){
            return self::fail('参数错误');
        }
        $res = Agent::where('id',$aid)->update(['phone'=>$phone]);
        if ($res != false){
            $field = ['id','nick_name','phone','avatar','company','new_house_num','second_hand_house_num','rent_house_num','is_first'];
            $data = Agent::where('id',$aid)->first($field)->toArray();
            $data['id'] = encrypt($data['id']);
            return self::success('登陆成功',$data);
        }else{
            return self::fail('登录失败');
        }

    }
}
